﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using ADial;
using Asterisk.NET.Manager;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using Spring.Context;
using Spring.Context.Support;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Spring.Aop;
using Exception=System.Exception;

namespace ADail
{
    public partial class ThisAddIn
    {
        // A reference to the Outlook Explorer collection to receive NewExplorer events.
        private Outlook.Explorers _explorers;

        // A collection of Outlook Explorer UI objects.
        // Each object adds and removes the UI for a specific Outlook Explorer window.
//        private List<CallOptions> _callOptionsList;

        private static CallManager _callManager = null ;



        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.ItemContextMenuDisplay += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
//            _callOptionsList = new List<CallOptions>();

            // For each existing Outlook Explorer, attach a new UI object.
//            foreach (Outlook.Explorer explorer in this.Application.Explorers)
//            {
//                _callOptionsList.Add(new CallOptions(explorer, _callOptionsList, false));
//            }

            // Subscribe to the NewExplorer event.
//            _explorers = this.Application.Explorers;
//            _explorers.NewExplorer += delegate(Microsoft.Office.Interop.Outlook.Explorer explorer)
//            {
//                _callOptionsList.Add(new CallOptions(explorer, _callOptionsList, true));
//            };
            startManager();
        }

        

        void Application_ItemContextMenuDisplay(Microsoft.Office.Core.CommandBar commandBar, Microsoft.Office.Interop.Outlook.Selection Selection)
        {

            GC.Collect();
            
            foreach (object o in Selection)
            {
                //string msgClass = o.MessageClass;

                //if(msgClass.Equals("IPM.Contact"))
                if(o is Outlook.ContactItem)
                {
                    Outlook.ContactItem item = (ContactItem)o;
            


                    addTelephoneNumber(commandBar,"Assistant",item.AssistantTelephoneNumber);
                    addTelephoneNumber(commandBar,"Business2",item.Business2TelephoneNumber);
                    addTelephoneNumber(commandBar,"Business",item.BusinessTelephoneNumber);
                    addTelephoneNumber(commandBar,"Callback",item.CallbackTelephoneNumber);
                    addTelephoneNumber(commandBar,"Car",item.CarTelephoneNumber);
                    addTelephoneNumber(commandBar,"Company",item.CompanyMainTelephoneNumber);
                    addTelephoneNumber(commandBar,"Home2",item.Home2TelephoneNumber);
                    addTelephoneNumber(commandBar,"Home",item.HomeTelephoneNumber);
                    addTelephoneNumber(commandBar,"Mobile",item.MobileTelephoneNumber);
                    addTelephoneNumber(commandBar,"Other",item.OtherTelephoneNumber);
                    addTelephoneNumber(commandBar,"Primary",item.PrimaryTelephoneNumber);
                    addTelephoneNumber(commandBar,"Radio",item.RadioTelephoneNumber);
                    addTelephoneNumber(commandBar,"TelexNumber",item.TelexNumber);
                    addTelephoneNumber(commandBar,"TTYTDD",item.TTYTDDTelephoneNumber);
                    

                    
                    
                    //button.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(button_Click);

                }
                
                
            }
            

        }
        private void addTelephoneNumber(Microsoft.Office.Core.CommandBar commandBar, string name, string number)
        {
            if (number != null && number.Trim().Length > 0)
            {
                
                CommandBarButton button = commandBar.Controls.Add(Office.MsoControlType.msoControlButton,
                                                                  missing, missing, missing, missing) as
                                              Office.CommandBarButton;
                button.Caption = "Call " + name + ": " + number;
                button.Tag = number;
                button.Visible = true;
                    
                button.Click += new Office
                    ._CommandBarButtonEvents_ClickEventHandler(click_dial);
            }
        }

        private void click_dial(CommandBarButton Ctrl, ref bool CancelDefault)
        {
            
            //MessageBox.Show("Dialing: " + Ctrl.Tag);
            _callManager.dial(Ctrl.Tag);
            
        }

        public ManagerConnection ManagerConnection = null;
        private void startManager()
        {
            try
            {
                IApplicationContext ctx = ContextRegistry.GetContext();
                object o = ctx.GetObject("manager");
                _callManager = (CallManager) ctx.GetObject("callManager");

                ManagerConnection connection = (ManagerConnection) o;
                this.ManagerConnection = connection;
                if (!connection.IsConnected()) connection.Login();
            }
            catch(Exception err)
            {
                MessageBox.Show("Error loading ADial (Asterisk Dialer)" + err.Message);
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Detach all Outlook Explorer UI objects from their respective windows.
//            foreach (CallOptions callOptions in _callOptionsList)
//            {
//                callOptions.Detach();
//            }
            if (ManagerConnection != null && ManagerConnection.IsConnected())
            {
                ManagerConnection.Logoff();
            }
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        


        #endregion
    }
}
